<?php
/*
 * Controlador de registro de usuarios en el sistema
 *
 * Autores: 
 *                - Fuck.inc
 */
 
class RegisterController extends AppController
{	
	public $codeConfirmation = "";
	public $registerData = null;
	
	// Muestra la vista por defecto
	public function index()
	{
	}
	
	// Registro de usuarios
	public function register()
	{
		if (Input::hasPost("register"))
		{
			$this->registerData = Input::post("register");
			$this->users->dump_result_self($this->registerData);
			try
			{
				$this->codeConfirmation = sha1($this->registerData['email'] . rand());
				$this->users->password = sha1($this->users->password);
				$this->users->dateRegister = date("Y-m-d H:i:s");
				$this->users->codeConfirmation = $this->codeConfirmation;
				$this->users->IPLastVisit = $_SERVER["REMOTE_ADDR"];
				$this->users->image = "user.png";
				
				if ($this->users->save())
				{
					if (!$this->verify_email())
					{
						Flash::error(ucfirst(_("there was an error sending the email message")) . ". " . ucfirst(_("please, try again later")) . ".");
						echo "Eliminar de la base de datos realizar log" . $this->mail->ErrorInfo;
						View::select(null);
					}	
				}
				else
				{
					Flash::error(ucfirst(_("there was an error saving the data")) . ". " . ucfirst(_("please, try again later")) . ".");
					View::select(null);
				}
			}
			catch(Exception $e)
			{
				Flash::error("Exception: {$e->getMessage()}");
				View::select(null);
			}
		}
		else
			Router::redirect("register"); // Se envía a la página principal, ya que está ingresando directamente la url
	}
	
	// Verifica que el campo (account ó email ) no se encuentre en el sistema
	public function verify_field()
	{
		$this->respuesta = array("error" => false, "message" => "");
		
		if(Input::hasPost("register"))
		{
			$this->registerData = Input::post("register");
			if  (!empty($this->registerData['account']))
			{
				if ($this->users->find_first("conditions: account='" . $this->registerData['account'] . "'"))
					$this->respuesta = array("error" => true, "message" => ucfirst(_("the user account already exists. Use a different user name for the new user account")) . ".");
			}
			elseif (!empty($this->registerData['email']))
			{
				if ($this->users->find_first("conditions: email='" . $this->registerData['email'] . "'"))
					$this->respuesta = array("error" => true, "message" => ucfirst(_("the e-mail address already exists. Use a different e-mail address")) . ".");
			}
			else
				$this->respuesta = array("error" => true, "message" => ucfirst(_("the request couldn't be processed")) . ".");
		}
		else
			Router::redirect(""); // Se envía a la página principal, ya que está ingresando directamente la url
			
		View::response('view');
	}
	
	// Enviar e-mail de verificación de registro
	public function verify_email()
	{
		$fullname = $this->registerData['firstname'] . " " . $this->registerData['lastname'];
		$mensaje = $this->em->verify_email($fullname, $this->registerData['account'], $this->codeConfirmation);
		$this->mail->AddAddress($this->registerData['email'], $fullname);
		$this->mail->Subject = ucfirst(_("verify your registration")) . " - " . $this->nameApp;
		$this->mail->Body = $mensaje;
		
		return $this->mail->Send();
	}
}
